#ifndef SINGLETON_H
#define SINGLETON_H

#include "global.h"

// 单例类
template <typename T> class Singleton {
protected:
  Singleton() = default;
  Singleton(const Singleton<T> &) = delete;
  Singleton &operator=(const Singleton<T> &st) = delete;

  // 类的static 变量 必须要初始化，如果不是模板类，则需要放在Cpp 里面，否则放在
  // .h 文件
  static std::shared_ptr<T> _instance;

public:
  static std::shared_ptr<T> GetInstance() {

    static std::once_flag s_flag;

    std::call_once(s_flag, [&]() { _instance = std::shared_ptr<T>(new T); });

    return _instance;
  }

  void PreintAddress() { std::cout << _instance.get() << std::endl; }

  ~Singleton() { std::cout << "this is singleton destruct" << std::endl; }
};

template <typename T>
std::shared_ptr<T> Singleton<T>::_instance = nullptr;

#endif // SINGLETON_H
